package cn.net.csit.platform_cloud.basicmodel.controller;

import cn.net.csit.platform_cloud.base.PtColumnModel;
import cn.net.csit.platform_cloud.base.PtColumnType;
import cn.net.csit.platform_cloud.basicmodel.domain.BasicObjDictionaryDomain;
import cn.net.csit.platform_cloud.basicmodel.service.IBackupDataService;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicObjOrganTypeService;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicObjectDictionaryService;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicObjectPropertyRelationService;
import cn.net.csit.platform_cloud.utils.BaseResult;
import cn.net.csit.platform_cloud.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/ObjDict")
@RestController
public class BasicObjDictionaryController {
    @Autowired
    IBackupDataService backupDataService;
    @Autowired
    IBasicObjectDictionaryService iBasicObjectDictionaryService;
    @Autowired
    IBasicObjectPropertyRelationService basicObjectPropertyRelationService;

    @Autowired
    IBasicObjOrganTypeService iBasicObjOrganTypeService;

    //查询所有记录--不分页--getAll
    @GetMapping("/getAll")
    public BaseResult getAll(){
        List<BasicObjDictionaryDomain> list=iBasicObjectDictionaryService.getAll();
        return ResultUtil.success(list);
    }

    //查询所有记录--分页--有条件
    @PostMapping("/getAllByChoosePage")
    public BaseResult getAllByChoosePage(Pageable pageable,String objName){
        PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        Map<String,String> map=new HashMap<>();
        if(null!=objName && ""!=objName){
            map.put("objName",objName);
        }

        List<BasicObjDictionaryDomain> list=iBasicObjectDictionaryService.getAll(map);
        PageInfo<BasicObjDictionaryDomain> info=new PageInfo<>(list);
        return ResultUtil.success(info);
    }
    //新增一条记录
    @PostMapping("/insert")
    public BaseResult insert(@RequestBody BasicObjDictionaryDomain record){
        int res=iBasicObjectDictionaryService.insert(record);
        return ResultUtil.success(res);
    }
    //根据主键查询一条记录
    @PostMapping("/getByPK")
    public BaseResult getByPK(String uuid){

        BasicObjDictionaryDomain model = iBasicObjectDictionaryService.selectByPrimaryKey(uuid);
        return ResultUtil.success(model);
    }
    //
    //查询组织类型下拉框　
    @GetMapping("/getAllOrganType")
    public BaseResult getAllOrganType(){
        return ResultUtil.success(iBasicObjOrganTypeService.getAllOrganType());
    }
    //根据主键修改一条记录
    @PostMapping("/updateByPK")
    public BaseResult updateByPrimaryKey(@RequestBody BasicObjDictionaryDomain record){
        return ResultUtil.success(iBasicObjectDictionaryService.updateByPrimaryKey(record));
    }
    //根据主键删除一条记录
    @PostMapping("/deleteByPK")
    public BaseResult deleteByPrimaryKey(String uuid){
        basicObjectPropertyRelationService.deleteByObjUuid(uuid);
        return ResultUtil.success(iBasicObjectDictionaryService.deleteByPrimaryKey(uuid));
    }
    //根据主键查询对象名称
    @PostMapping("/getObjName")
    public BaseResult  getObjName(String uuid){
        Map<String,String> map=new HashMap<>();
        map.put("uuid",uuid);
        //根据条件查询后台数据库，获取对象名称
        Map<String,String> map1=iBasicObjectDictionaryService.getObjName(map);
        //将数据返回到页面显示
        return ResultUtil.success(map1);
    }
    //根据主键查询父对象uuid
    @GetMapping("/getObjParentUuid")
    public  BaseResult  getObjParentUuid(){
        //根据条件查询后台，获取父对象uuid
        List<Map<String, String>> map1 = iBasicObjectDictionaryService.getObjParentUuid();
        return ResultUtil.success(map1);
    }

    @GetMapping("createTable")
    public  void createTable(String tableName){
        List<Map<String,String>> columns=new ArrayList<>();
        Map<String,String> map1=new HashMap<>();
        map1.put("code","obj_instance_code");
        map1.put("type","STRING");
        map1.put("length","64");
        columns.add(map1);

        Map<String,String> map2=new HashMap<>();
        map2.put("code","obj_instance_name");
        map2.put("type","STRING");
        map2.put("length","64");
        columns.add(map2);
        backupDataService.createTable(tableName,columns);
    }
    @GetMapping("createNullTable")
    public  void createNullTable(String tableName){
        backupDataService.createNullTable(tableName);
    }

    @GetMapping("deleteTable")
    public  void deleteTable(){
        String s="test2";
        backupDataService.deleteTable(s);
    }
    @GetMapping("truncateTable")
    public  void truncateTable(){
        String s="test2";
        backupDataService.truncateTable(s);
    }

    @GetMapping("addColumnList")
    public  void addColumnList(String tableName){
        List<String> columnNameList=new ArrayList<>();
        columnNameList.add("user_name");
        columnNameList.add("user_password");
        backupDataService.addColumnList(tableName,columnNameList);
    }
    @GetMapping("addColumns")
    public  void addColumns(String tableName){
        List<PtColumnModel> columnNameList=new ArrayList<>();
        PtColumnModel model=new PtColumnModel();
        model.setColName("user_password");
        model.setColType(PtColumnType.STRING);
        model.setColLength(20);
        columnNameList.add(model);

        backupDataService.addColumns(tableName,columnNameList);
    }
    @GetMapping("delColumns")
    public  void delColumns(){
        String tableName="test2";
        List<String> columns=new ArrayList<>();
        columns.add("obj_desc");
        backupDataService.delColumns(tableName,columns);
    }

    @GetMapping("isTargetTableExistInDB")
    public  String isTargetTableExistInDB(){
        String dataBaseName="basic_model";
        String NewTableName="test2";
        String res=backupDataService.isTargetTableExistInDB(dataBaseName,NewTableName);
        return res;
    }
}
